package cn.kgc.controller;

import cn.kgc.base.dto.UmsRoleDTO;
import cn.kgc.commons.vo.Result;
import cn.kgc.entity.UmsMemberRole;
import cn.kgc.entity.UmsRole;
import cn.kgc.service.UmsMemberRoleService;
import cn.kgc.service.UmsRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p></p>
 *
 * @author 李孟鸠
 * @Date: 2022年12月08日9:24
 */
@RestController
@RequestMapping("/ums/role")
@Validated
@Api(tags = "角色控制器")
public class RoleController {
    @Resource
    private UmsMemberRoleService umsMemberRoleService;
    @Resource
    private UmsRoleService umsRoleService;

    @GetMapping("/getRole")
    @ApiOperation(value = "根据用户的ID获取角色信息",httpMethod = "GET",
            produces = "application/json;charset=UTF-8",response = Result.class)
    @CrossOrigin
    @ApiImplicitParam(name="memberId",value="会员ID",dataType = "long",paramType = "query")
    public List<UmsRoleDTO> getUmsRole(@RequestParam(name = "memberId") long memberId){
        List<UmsRoleDTO>umsRoles=new ArrayList<>();
        //查询用户对应的角色的ID
        LambdaQueryWrapper<UmsMemberRole>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsMemberRole::getMemberId,memberId);
        List<UmsMemberRole> umsMemberRoleList =
                umsMemberRoleService.list(lambdaQueryWrapper);
        //获得每个角色的信息
        for (UmsMemberRole umsMemberRole : umsMemberRoleList) {
            long roleId = umsMemberRole.getRoleId();
            UmsRole umsRole = umsRoleService.getById(roleId);
            UmsRoleDTO umsRoleDTO =new UmsRoleDTO();
            BeanUtils.copyProperties(umsRole,umsRoleDTO);
            umsRoles.add(umsRoleDTO);
        }
        return umsRoles;
    }
}
